namespace CIS.Resource
{
    using CISLib;
    using System;
    using System.IO;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;

    public class ResourceDirectoryFileSharePage : Page
    {
        protected Button addBtn;
        protected TextBox FileNameTxb;
        protected Image iResource;
        protected Label lbFileName;
        protected UserPrincipal Principal = null;
        protected Repeater rptFile;
        protected HtmlInputFile UpdateImageFile;

        private void addBtn_Click(object sender, EventArgs e)
        {
            if (this.FileNameTxb.Text.Trim() != string.Empty)
            {
                try
                {
                    int FileSize = this.UpdateImageFile.PostedFile.ContentLength;
                    string FileType = this.UpdateImageFile.PostedFile.ContentType;
                    Stream FileStream = this.UpdateImageFile.PostedFile.InputStream;
                    string strFilePostfixName = this.UpdateImageFile.PostedFile.FileName;
                    int pos = strFilePostfixName.LastIndexOf(".");
                    strFilePostfixName = strFilePostfixName.Substring(pos);
                    byte[] FileContent = new byte[FileSize];
                    int Status = FileStream.Read(FileContent, 0, FileSize);
                    string strFileNameTxb = this.FileNameTxb.Text.Trim();
                    ManageResource.InsertNewFileStorgeAndInfo(new Guid(base.Request.QueryString["DirectoryId"]), FileType, FileContent, strFileNameTxb, strFilePostfixName);
                    this.FileNameTxb.Text = string.Empty;
                }
                catch (Exception)
                {
                    return;
                }
                this.rptFileDataBind();
            }
        }

        private void DownloadDoc(Guid docId)
        {
            ResourceDirectoryFileStorge resourceDirectoryFileStorge = ManageResource.GetResourceDirectoryFileStorgeViaId(docId);
            ResourceDirectoryFileInfo resourceDirectoryFileInfo = ManageResource.GetResourceDirectoryFileInfoViaId(docId);
            base.Response.ContentType = "application/octet-stream";
            base.Response.AddHeader("Content-Disposition", "attachment; filename=" + resourceDirectoryFileInfo.FileName + resourceDirectoryFileStorge.FilePostfixName);
            base.Response.AddHeader("Content-Transfer-Encoding", "binary");
            base.Response.OutputStream.Write(resourceDirectoryFileStorge.FileStorge, 0, resourceDirectoryFileStorge.FileStorge.Length);
            base.Response.OutputStream.Flush();
            base.Response.End();
        }

        private void InitializeComponent()
        {
            this.addBtn.Click += new EventHandler(this.addBtn_Click);
            this.rptFile.ItemCommand += new RepeaterCommandEventHandler(this.rptFile_ItemCommand);
            base.Load += new EventHandler(this.Page_Load);
        }

        private bool IsDelete()
        {
            if (this.Principal.OwnRole.TypeC == 0)
            {
                base.Response.Write("<script> alert('没有权限操作！') </script>");
                return false;
            }
            return (this.Principal.OwnRole.TypeC == 1);
        }

        protected override void OnInit(EventArgs e)
        {
            this.InitializeComponent();
            base.OnInit(e);
            this.Principal = this.Context.User as UserPrincipal;
        }

        private void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                this.SelectAuth();
                this.SetFromImage();
                this.SetFrom();
                this.rptFileDataBind();
            }
        }

        private void rptFile_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            string cmd = e.CommandName.ToString();
            Guid docId = new Guid(e.CommandArgument.ToString());
            switch (cmd)
            {
                case "download":
                    this.DownloadDoc(docId);
                    break;

                case "delete":
                    if (!this.IsDelete())
                    {
                        return;
                    }
                    ManageResource.RemoveFileStorgeAndInfo(docId);
                    break;
            }
            this.rptFileDataBind();
        }

        private void rptFileDataBind()
        {
            this.rptFile.DataSource = ManageResource.GetDirectoryFileInfo(new Guid(base.Request.QueryString["DirectoryId"]));
            this.rptFile.DataBind();
        }

        private void SelectAuth()
        {
            if (this.Principal.OwnRole.TypeC == 0)
            {
                this.UpdateImageFile.Visible = false;
                this.FileNameTxb.Visible = false;
                this.lbFileName.Visible = false;
                this.addBtn.Visible = false;
            }
            else if (this.Principal.OwnRole.TypeC == 1)
            {
                this.UpdateImageFile.Visible = true;
                this.FileNameTxb.Visible = true;
                this.lbFileName.Visible = true;
                this.addBtn.Visible = true;
            }
        }

        private void SetFrom()
        {
            string language = this.Session["language"].ToString();
            string path = base.Server.MapPath("~/CIS/LanguageConfig.xml");
            this.lbFileName.Text = ManageSystemConfig.GetSystemSetLanguage("ResourceDirectoryFileSharePage", "lbFileName", language, path);
        }

        private void SetFromImage()
        {
            switch (this.Session["language"].ToString())
            {
                case "cn":
                    this.iResource.ImageUrl = "../images/title_SharedInfo.jpg";
                    break;

                case "en":
                    this.iResource.ImageUrl = "../images/title_SharedInfoEn.jpg";
                    break;
            }
        }
    }
}

